﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibSolPedI;
using WebLibSolPedBE;
using SAP.Middleware.Connector;

namespace LibSolPedDALC
{
    public class SolicitudAnticipoDALC : ISolAnticipo
    {
        private SolicitudAnticipoDALC() { }
        private static volatile SolicitudAnticipoDALC _instance = null;

        public static SolicitudAnticipoDALC Instance()
        {
            if (_instance == null)
            {
                lock (typeof(SolicitudAnticipoDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new SolicitudAnticipoDALC();
                    }
                }
            }
            return _instance;
        }

        public List<SolAnticipoBE> getLstSolAnticipo()
        {
            List<SolAnticipoBE> lst = new List<SolAnticipoBE>();

            IRfcFunction function = null;
            RfcDestination dest = null;
            DestConfig dc = new DestConfig();
            try
            {
                function = DALC.rfcRep.CreateFunction("Z_MOVIL_FI_GET_SOLIC_ANTI_CAB");

                function.Invoke(DALC.rfcDest);
                IRfcTable T_RETORNO = function.GetTable("EP_TRETURN");
                foreach (IRfcStructure item in T_RETORNO)
                {
                    SolAnticipoBE it = new SolAnticipoBE();
                    it.BELNR = item["BELNR"].GetString();
                    it.LIFNR = item["LIFNR"].GetString();
                    it.DLIFNR = item["DLIFNR"].GetString();
                    it.ZUONR = item["ZUONR"].GetString();
                    it.WAERS = item["WAERS"].GetString();
                    it.DMBTR = item["DMBTR"].GetString();
                    it.BUDAT = item["BUDAT"].GetString();
                    it.BUKRS = item["BUKRS"].GetString();
                    it.GJAHR = item["GJAHR"].GetString();
                    it.SEGMENT = item["SEGMENT"].GetString();
                    it.XREF2_HD = item["XREF2_HD"].GetString();
                    it.VALOR1 = item["VALOR1"].GetString();
                    it.VALOR2 = item["VALOR2"].GetString();
                    it.VALOR3 = item["VALOR3"].GetString();
                    it.VALOR4 = item["VALOR4"].GetString();
                    it.VALOR5 = item["VALOR5"].GetString();
                    lst.Add(it);
                }
            }
            catch (Exception e)
            {

            }

            return lst;
        }

        public string setAprobarSolAnticipo(List<SolAnticipoBE> lst)
        {
            String ret = "Resultado de libetación: <br>" + Environment.NewLine;
            try
            {
                foreach (SolAnticipoBE it in lst)
                {
                    IRfcFunction function = null;
                    function = DALC.rfcRep.CreateFunction("Z_MOVIL_FI_APROB_SOLIC_ANTI");
                    function.SetValue("IP_BELNR", it.BELNR);
                    function.SetValue("IP_BUKRS", it.BUKRS);
                    function.SetValue("IP_GJAHR", it.GJAHR);
                    function.SetValue("IP_SEGMENTO", it.SEGMENT);
                    function.SetValue("IP_XREF2_HD", it.XREF2_HD);
                    function.SetValue("IP_DMBTR", it.DMBTR);
                    function.Invoke(DALC.rfcDest);
                    IRfcTable T_RETORNO = function.GetTable("TR_RETORNO");
                    foreach (IRfcStructure item in T_RETORNO)
                    {
                        ret = ret + "  -" + item["MESSAGE"].GetString() + ". <br>";
                    }
                    ret = ret + "<br>" + Environment.NewLine;
                }
            }
            catch (Exception ex)
            {
                ret = ret + "<br>" + "Error en el resto de documentos";
            }
            return ret;
        }
    }
}
